package org.yuxiong.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

@SuppressWarnings("unused")
public class HttpUtils {
	
	private static final int TIMEOUT = 200000;
	private static URL objUrl = null;
	private static HttpURLConnection conn = null;
	private static String readInfo = null;
	private static final String SYMBOL_EMPTY = "";
	private static StringBuffer rtnSb = new StringBuffer(SYMBOL_EMPTY);
	private static final String STR_LOG = "CALL URL -> ";
	private static final String SYMBOL_BLANK = " ";
	private static final String SYMBOL_20 = "%20";
	private static final String STR_REFERER = "Referer";
	
	
	private static void init(String url) throws DownloadException {
		System.out.println(STR_LOG+url);
			try {
				url = url.trim().replaceAll(SYMBOL_BLANK, SYMBOL_20);
				objUrl = new URL(url);
				conn = (HttpURLConnection) objUrl.openConnection();
				 conn.setConnectTimeout(TIMEOUT);
				 conn.setReadTimeout(TIMEOUT);
				 conn.connect();
			} catch (MalformedURLException e) {
				throw new DownloadException(e);
			} catch (IOException e) {
				close(conn);
				throw new DownloadException(e);
			}
	}
	
	private static void init(String url, Proxy proxy) throws DownloadException {
		System.out.println("proxy->"+proxy+",  "+STR_LOG+url);
			try {
				url = url.trim().replaceAll(SYMBOL_BLANK, SYMBOL_20);
				objUrl = new URL(url);
				conn = (HttpURLConnection) objUrl.openConnection(proxy);
				 conn.setConnectTimeout(TIMEOUT);
				 conn.setReadTimeout(TIMEOUT);
				 conn.connect();
			} catch (MalformedURLException e) {
				throw new DownloadException(e);
			} catch (IOException e) {
				close(conn);
				throw new DownloadException(e);
			}
	}
	
	private static void free(BufferedReader in,HttpURLConnection conn) throws IOException{
		close(in,conn);
		rtnSb = new StringBuffer(SYMBOL_EMPTY);
		readInfo = null;
	}
	
	private static void close(BufferedReader in,HttpURLConnection conn) throws IOException{
		if(null!=in){
			in.close();
		}
		if(null!=conn){
			conn.disconnect();
		}
	}
	
	private static void close(HttpURLConnection conn) {
		if(null!=conn){
			conn.disconnect();
		}
	}
	
	public static String callURL(String url, String sEncode) throws DownloadException{
		init(url);
		String rtnStr = null;
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),sEncode));
			while ((readInfo = in.readLine()) != null) 
			 {
				 rtnSb.append(readInfo);
			 } 
			rtnStr = rtnSb.toString();
			free(in,conn);
		} catch (IOException e) {
			close(conn);
			throw new DownloadException(e);
		}
		if(SuperString.isBlank(rtnStr)) return null;
		else return rtnStr;
	} 
	
	public static String callURL(String url, String sEncode, Proxy proxy) throws DownloadException{
		init(url, proxy);
		String rtnStr = null;
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),sEncode));
			while ((readInfo = in.readLine()) != null) 
			 {
				 rtnSb.append(readInfo);
			 } 
			rtnStr = rtnSb.toString();
			free(in,conn);
		} catch (IOException e) {
			close(conn);
			throw new DownloadException(e);
		}
		if(SuperString.isBlank(rtnStr)) return null;
		else return rtnStr;
	}
	public static void main(String args[]) throws MalformedURLException, IOException {
			

		
	}
}
